package com.wondersgroup.insurance.dnuilearn.combination.scrollanima;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * Created by Administrator on 2017/6/20.
 */

public class MyScrollView extends ScrollView {
    MyLinearLayout linearLayout = null;

    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        linearLayout = (MyLinearLayout) getChildAt(0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (linearLayout.getChildCount() > 0){
            View first = linearLayout.getChildAt(0);
            first.getLayoutParams().height = getHeight();
        }

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        int size = linearLayout.getChildCount();
        if (size>0){
            int scrollViewHeight = getHeight();
            //监听滑动的程度---childView从下面冒出来多少距离/childView.getHeight();----0~1：动画执行的百分比ratio
            //动画执行的百分比ratio控制动画执行

            for (int i=0;i<size;i++){
                View child = linearLayout.getChildAt(i);
                int childHeight = child.getHeight();

                if (! (child instanceof DiscrollInterface)){
                    continue;
                }
                //接口回掉，传递执行的百分比给MyFrameLayout
                //低耦合高内聚
                //            MyFrameLayout mf = (MyFrameLayout) child;
                DiscrollInterface discrollInterface = (DiscrollInterface) child;
                //child离parent顶部的高度
                int childTop = child.getTop();
                //滑出去的这一截高度：t
//            child离屏幕顶部的高度
                int absoluteTop = childTop -t;
                int childBottom = child.getBottom();
                if (childBottom > t&& absoluteTop <= scrollViewHeight){
                    //child浮现的高度 = ScrollView的高度 - child离屏幕顶部的高度
                    int visibleGap = scrollViewHeight - absoluteTop;
                    //float ratio = child浮现的高度/child的高度
                    float ratio = visibleGap /(float)  childHeight;
                    //确保ratio是在0~1的范围
                    discrollInterface.onDiscroll(clamp(ratio,1f,0f));
                }else {
                    discrollInterface.onResetDiscroll();
                }

            }
        }

    }

    //求三个数的中间大小的一个数。
    public static float clamp(float value, float max, float min){
        return Math.max(Math.min(value, max), min);
    }
}
